package com.controller;

import com.entity.FAddress;
import com.entity.FOrder;
import com.entity.FUser;
import com.entity.table.FAddressTableDef;
import com.entity.table.FOrderTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.FAddressService;
import com.service.FOrderService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserController {
    @Autowired
    private FOrderService fOrderService;
    @Autowired
    private FAddressService fAddressService;

    @GetMapping("/user_center_info")
    public String user_center_info(HttpServletRequest request, ModelMap model){
        HttpSession session = request.getSession();
        FUser user = (FUser) session.getAttribute("user");
        model.addAttribute("user",user);
        return "client/user_center_info";
    }

    @GetMapping("user_center_site")
    public String user_center_site(HttpServletRequest request, ModelMap model){
        HttpSession session = request.getSession();
        FUser user = (FUser) session.getAttribute("user");
        FAddress fAddress=fAddressService.queryChain().where(FAddressTableDef.F_ADDRESS.USER_ID.eq(user.getId())).one();
        model.addAttribute("fAddress",fAddress);
        return "client/user_center_site";
    }

    @PostMapping("/fAddress/update")
    public String update(FAddress fAddress, ModelMap model,HttpServletRequest request){
        HttpSession session = request.getSession();
        FUser user = (FUser) session.getAttribute("user");
        fAddress.setUserId(user.getId());
        model.addAttribute("fAddress",fAddress);
        fAddressService.saveOrUpdate(fAddress);
        return "client/user_center_site";
    }
    @GetMapping("/user_center_order")
    public String userOrderList(ModelMap model, HttpServletRequest request,
                                @RequestParam(value = "p",defaultValue = "1")int p,
                                @RequestParam(value = "pageSize",defaultValue = "10")int pageSize) {
        try {
            HttpSession session = request.getSession();
            FUser user = (FUser) session.getAttribute("user");
            Page<FOrder> page = new Page<>();
            page.setPageSize(pageSize);
            page.setPageNumber(p);
            QueryWrapper wrapper = QueryWrapper.create().where(FOrderTableDef.F_ORDER.USER_ID.eq(user.getId()));
            Page<FOrder> orderList = fOrderService.getMapper().paginateWithRelations(page,wrapper);
            model.addAttribute("page",orderList);
            model.addAttribute("user",user);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "client/user_center_order";
    }
}
